package com.sgb.kjwl.view.ui.activity.self.packers;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AtSelfPackersManage2Binding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.FindPackersEntity;
import com.sgb.kjwl.utils.ActivityJump;
import com.sgb.kjwl.view.ui.adapter.self.packers.SelfPackersManageAdapter;
import com.sgb.link.view.widget.XLinearLayoutManager;
import com.sgb.kjwl.viewmodel.self.packers.SelfPackersManageModel2;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.CommonDialog;
import com.swgk.core.helper.OnMultiClickListener;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;
import java.util.List;
import javax.inject.Inject;

/**
 * description:个人中心-工队列表
 * @author PC
 */
public class SelfPackersManageActivity2 extends AppActivity {

    @Inject
    SelfPackersManageModel2 viewModel;
    private AtSelfPackersManage2Binding binding;
    private SelfPackersManageAdapter mAdapter;
    public static final int REQUEST_EDIT_CODE = 100321;
    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, SelfPackersManageActivity2.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().
                appComponent(appComponent).selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActionBar.setMenuFunctionClick(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                Intent intent = new Intent(getActivity(), SelfTeamIntroduceActivity.class);
                startActivityForResult(intent, REQUEST_EDIT_CODE);
            }
        });
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.at_self_packers_manage2);
        binding.setViewModel(viewModel);
        if (mAdapter == null) {
            String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
            if ("".equals(companyNo) || getString(R.string.app_zero).equals(companyNo)) {
                mAdapter = new SelfPackersManageAdapter(this, null, 0);
            } else {
                mAdapter = new SelfPackersManageAdapter(this, null, 1);
            }
        }
        binding.recyclerView.setLayoutManager(new XLinearLayoutManager(this));
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.pageTeamInfo(false);
            }

            @Override
            public void onLoadMore() {
                viewModel.pageTeamInfo(true);
            }
        });
        binding.recyclerView.setAdapter(mAdapter);
        mAdapter.setWorkQueueTableDispose(new SelfPackersManageAdapter.WorkQueueTable() {
            @Override
            public void itemData(int position) {
                viewModel.TeamParticulars(mAdapter.getDatas().get(position).getNo());
            }

            @Override
            public void edit(int position) {
                int teamType = mAdapter.getDatas().get(position).getTeamType();
                ActivityJump.jumpToActivityForResult(SelfPackersManageActivity2.this, SelfTeamIntroduceActivity.class,REQUEST_EDIT_CODE
                        , Pair.create("teamType",teamType+""),Pair.create("no",mAdapter.getDatas().get(position).getNo()));
            }

            @Override
            public void delete(int position) {
                //删除点击事件
                affirmPop(mAdapter.getDatas().get(position));
            }
        });
    }

    @Override
    protected void init() {
        viewModel.pageTeamInfo(false);
    }

    @Override
    protected void onResume() {
        super.onResume();
        viewModel.pageTeamInfo(false);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_EDIT_CODE) {
                viewModel.pageTeamInfo(false);
            }
        }
    }

    /**
     * 接口访问出错时
     */
    public void showEmpty() {
        binding.recyclerView.loadMoreComplete();
        binding.recyclerView.refreshComplete();
        binding.emptyView.setVisibility(View.VISIBLE);
        binding.recyclerView.setLoadingMoreEnabled(false);
    }

    public void showList(ListResponse<List<FindPackersEntity>> data, boolean isLoadMore, boolean isLast) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (data != null && data.getList().size() > 0) {
                mAdapter.addDatas(data.getList());
            }
            binding.recyclerView.setNoMore(isLast);
        } else {
            binding.recyclerView.refreshComplete();
            if (data != null && data.getList().size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                mAdapter.setDatas(data.getList());
                binding.recyclerView.setNoMore(isLast);
            } else {
                mAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    private void affirmPop(FindPackersEntity entity) {
        new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                .setTitle("确认删除？")
                .setDialogCallBack(new CommonDialog.DialogCallBack() {
                    @Override
                    public void onRightClick() {
                        viewModel.deleteTeam(entity);
                    }

                    @Override
                    public void onLeftClick() {
                    }
                }).show();
    }
}
